#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int kMaxN = 1e5 + 10;
const long long kInf = 1e18;

vector<pair<int, int>> g[kMaxN];
int pre[kMaxN];
long long dist[kMaxN];

void Dijkstra(int n) {
  priority_queue<pair<long long, int>> pq;

  fill(dist, dist + n + 1, kInf);
  dist[1] = 0;
  pq.emplace(-dist[1], 1);
  while (!pq.empty()) {
    long long d = -pq.top().first;
    int u = pq.top().second;

    pq.pop();
    if (dist[u] != d) {
      continue;
    }
    for (const pair<int, int> &e : g[u]) {
      int v = e.first;

      if (dist[v] > d + e.second) {
        dist[v] = d + e.second;
        pre[v] = u;
        pq.emplace(-dist[v], v);
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, w;

    cin >> u >> v >> w;
    g[u].emplace_back(v, w);
    g[v].emplace_back(u, w);
  }
  Dijkstra(n);

  if (dist[n] == kInf) {
    cout << "-1\n";
  } else {
    vector<int> ans;
    int now = n;

    while (now) {
      ans.emplace_back(now);
      now = pre[now];
    }
    reverse(ans.begin(), ans.end());
    for (int id : ans) {
      cout << id << " ";
    }
    cout << "\n";
  }

  return 0;
}